//
//  YLProjectListController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/11/9.
//  Copyright © 2017年 she1323. All rights reserved.
//  项目列表

#import "YLProjectListController.h"
#import "YLProductListTableViewCell.h"
#import "YLGlobeHeader.h"
#import "YLProjectDetailViewController.h"
#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import <YYModel.h>
#import <MJRefresh.h>
#import "YLAccountTool.h"
#import "YLProductListModel.h"
@interface YLProjectListController ()
@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) NSMutableArray *models;
@end

@implementation YLProjectListController

- (NSMutableArray *)models
{
    if (!_models) {
        _models = [NSMutableArray array];
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"项目";
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLProductListTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLProductListTableViewCell cellID]];
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer.hidden = YES;
    
    [self.tableView.mj_header beginRefreshing];
    
    
}

- (void)loadData
{
    NSInteger count = 10;
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
    params[@"MaxResultCount"] = @(count);
    params[@"PageIndex"] = @(_index);
    __weak typeof(self) weakSelf = self;
    NSString *url = [NSString stringWithFormat:@"%@Product/PGetProjectList",YLURL];
    
    [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
        if ([responseObject[@"httpCode"] integerValue] == 200) {
            [weakSelf endRefresh];
            if (weakSelf.index == 1) {
                [weakSelf.models removeAllObjects];
            }
            
            YLProductListItem *item = [YLProductListItem yy_modelWithDictionary:responseObject[@"httpResult"]];
            
            [weakSelf.models addObjectsFromArray:item.item];
            
            if (item.count > weakSelf.models.count) {
                
            } else {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.tableView reloadData];
            
        } else {
            [WHProgressHUD showError:responseObject[@"message"]];
        }
    } fail:^(NSError *error) {
        [weakSelf endRefresh];
    }];
    
}

- (void)endRefresh
{
    self.tableView.mj_footer.hidden = NO;
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.models.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YLProductListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLProductListTableViewCell cellID] forIndexPath:indexPath];
    
    cell.model = self.models[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (YLScreenW - 20) * 9 / 16.0 - 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLProjectDetailViewController *detail = [[YLProjectDetailViewController alloc] init];
    detail.model = self.models[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

@end
